class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def max_depth(root):
    # 基线条件：空节点深度为0
    if not root:
        return 0

    # 递归计算左右子树深度
    left_depth = max_depth(root.left)
    right_depth = max_depth(root.right)

    # 当前节点的深度 = 1(自身) + 左右子树中较深的深度
    return 1 + max(left_depth, right_depth)


# 测试：构建一棵二叉树
#       3
#      / \
#     9  20
#       /  \
#      15   7
root = TreeNode(3)
root.left = TreeNode(9)
root.right = TreeNode(20)
root.right.left = TreeNode(15)
root.right.right = TreeNode(7)

print(max_depth(root))  # 输出: 3
